<?php
function send_simple_message($email,$title,$content,$id,$hostArr) {
    $email = trim($email);
	$title = trim($title);
	$content = trim($content);
	if(empty($email)||empty($title)||empty($content)){
		exit;
	}
    $mail = new PHPMailer();
    $mail->IsHTML(true);
    $mail->Charset="utf-8";
    $mail->IsSMTP();
    $mail->SMTPDebug = 0;
    $mail->SMTPAuth = true;
    if(array_key_exists("tls",$hostArr)){
		$mail->SMTPSecure = 'tls';
		$mail->SetFrom("cs@hairza.com", "HairExtensionKingdom");
	}else{
		$mail->SetFrom("edm@xxhair.com", "HairExtensionKingdom");
	}
    $mail->Host =  $hostArr['host'];
    $mail->Port = $hostArr['port'];
    $mail->Username = $hostArr['user'];
    $mail->Password = $hostArr['pwd'];    
    $mail->Subject = $title;
    $mail->Body = $content;
    $mail->AddAddress($email);
    #echo $email;
    if(!$mail->Send()){echo $error = 'Mail error: '.$mail->ErrorInfo;;exit;}else{echo "success!";}
}
/*将跟踪链接和退订链接都加上去
unsubUrlArr  退订的指定URL
userActionArr 用户行为
content 内容
email  邮件地址  
id  邮件ID
type  邮件种类
templateId 邮件模板ID
*/
function formatContent($unsubUrlArr,$userActionArr,$content,$email,$id,$type,$templateId,$name){
    $ip = "";$url = '';
    $userBrowser = "";
    $formateDate = date("Y-m-d H:i:s", time());
    $email = urlencode($email);
	if(empty($name)){
		$num = rand(0,1220);
		$name = $name[$num];
	}
	if(strpos($type,"6")==0){
		$url = $unsubUrlArr[0];
	}else{
		$url = $unsubUrlArr[1];
	}
    $unsubUrl = "http://".$url."/unsub.php?unsub=".base64_encode($email."|".$id."|".time()."|".$userBrowser."|".$ip."|".$type."|".$templateId);
	$trackUrl = "<img width=\"1\" height=\"1\" src=http://".$url."/open.php?m=".base64_encode($email."|".$id."|".$type."|".$templateId."|").".jpg>";
	$updateUrl = "http://".$url."/update.php?update=".base64_encode($email."|".$id."|".time()."|".$userBrowser."|".$ip."|".$type);
    $content = str_replace($userActionArr['unsub'],$unsubUrl, $content);
    $content = str_replace($userActionArr['track'],$trackUrl, $content);
    $content = str_replace($userActionArr['update'],$updateUrl, $content);
	$content = str_replace($userActionArr['name'],$name, $content);
    $content = str_replace($userActionArr['email'],$email."&addressId=".$id."&type=".$type, $content);
    $content = addslashes($content);
	return $content;
}
/*将跟踪链接和退订链接都加上去*/

function get_onlineip() {
    $onlineip = '';
    if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
        $onlineip = getenv('HTTP_CLIENT_IP');
    } elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
        $onlineip = getenv('HTTP_X_FORWARDED_FOR');
    } elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
        $onlineip = getenv('REMOTE_ADDR');
    } elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
        $onlineip = $_SERVER['REMOTE_ADDR'];
    }
    return $onlineip;
}

function getBrowserVersion(){
    return $_SERVER['HTTP_USER_AGENT'];
}


?>